package qy.jalgotrade.eventprof;

import java.time.ZonedDateTime;

import it.unimi.dsi.fastutil.doubles.DoubleArrayList;

/**
 * @author qy
 *
 */
public class Event {

	private ZonedDateTime __occurred;

	private int __lookBack;

	private int __lookForward;

	private DoubleArrayList __values;

	/**
	 * 
	 * @param occurred occurred
	 * @param lookBack lookBack
	 * @param lookForward lookForward
	 */
	public Event(ZonedDateTime occurred, int lookBack, int lookForward) {

		assert occurred != null && lookBack > 0 && lookForward > 0;
		__occurred = occurred;
		__lookBack = lookBack;
		__lookForward = lookForward;
		int len = __lookBack + __lookForward + 1;
		__values = new DoubleArrayList(len);
		for (int i = 0; i < len; i++) {
			__values.add(Double.NaN);
		}
	}

	/**
	 * 
	 * @param t the offset of event occured (-lookBack &lt;= t &lt;= lookForward)
	 * @return int
	 */
	private int __mapPos(int t) {

		assert t >= -1 * __lookBack && t <= __lookForward;
		return t + __lookBack;
	}

	/**
	 * 
	 * @return boolean
	 */
	public boolean isComplete() {

		return !__values.stream().anyMatch(e -> {
			return Double.isNaN(e);
		});
	}

	/**
	 * 
	 * @return __occured
	 */
	public ZonedDateTime getOccured() {

		return __occurred;
	}

	/**
	 * 
	 * @return __lookBack
	 */
	public int getLookBack() {

		return __lookBack;
	}

	/**
	 * 
	 * @return __lookForward
	 */
	public int getLookForward() {

		return __lookForward;
	}

	/**
	 * 
	 * @param t     the offset of event occured (-lookBack &lt;= t &lt;= lookForward)
	 * @param value value
	 */
	public void setValue(int t, double value) {

		if (!Double.isNaN(value)) {
			int pos = __mapPos(t);
			__values.set(pos, value);
		}
	}

	/**
	 * 
	 * @param t the offset of event occured (-lookBack &lt;= t &lt;= lookForward)
	 * @return __values
	 */
	public double getValue(int t) {

		int pos = __mapPos(t);
		return __values.getDouble(pos);
	}

	/**
	 * 
	 * @return __values
	 */
	public DoubleArrayList getValues() {

		return __values;
	}
}
